//
//  LJYRecDayTableViewCell.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/25.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYRecDayTableViewCell.h"

@interface LJYRecDayTableViewCell ()

@property(nonatomic, strong)UIImageView *titleImage;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *subscribeLabel;
@property(nonatomic, strong)UILabel *littleTitleLabel;
@property(nonatomic, strong)UILabel *detaailLabel;
@property(nonatomic, strong)UIButton *selectButton;

@end

@implementation LJYRecDayTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self creatAction];
    }
    return self;
}

-(void)creatAction
{
    _titleImage = [[UIImageView alloc] init];
    _titleImage.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:_titleImage];
    [_titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(self.contentView).offset(15);
        make.width.height.mas_equalTo(40);
        
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleImage);
        make.left.equalTo(self.titleImage.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-60);
        make.height.mas_equalTo(20);
        
    }];
    
    _subscribeLabel = [[UILabel alloc] init];
    _subscribeLabel.backgroundColor = [UIColor clearColor];
    _subscribeLabel.textColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    _subscribeLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:_subscribeLabel];
    [_subscribeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLabel.mas_bottom).offset(3);
        make.left.equalTo(self.titleImage.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-60);
        make.height.mas_equalTo(20);
        
    }];
    
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectButton];
    [_selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.subscribeLabel);
        make.left.equalTo(self.subscribeLabel.mas_right).offset(2);
        make.width.mas_equalTo(53);
        make.height.mas_equalTo(25);
        
    }];
    
    _littleTitleLabel = [[UILabel alloc] init];
    _littleTitleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_littleTitleLabel];
    [_littleTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleImage.mas_bottom).offset(10);
        make.left.equalTo(self.titleImage);
        make.right.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(10);
        
    }];
    
    _detaailLabel = [[UILabel alloc] init];
    _detaailLabel.backgroundColor = [UIColor clearColor];
    _detaailLabel.numberOfLines = 0;
    _detaailLabel.textColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    _detaailLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_detaailLabel];
    [_detaailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.littleTitleLabel.mas_bottom).offset(3);
        make.left.equalTo(self.titleImage);
        make.right.equalTo(self.contentView);
        make.bottom.mas_equalTo(-5);
        
    }];
}

-(void)setRecommend:(LJYRecEveryDayModel *)recommend
{
    
    if (_recommend != recommend)
    {
        _recommend = recommend;
    }
    [self.titleImage sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"u=1625671141,2113623839&fm=21&gp=0.jpg"]];
    self.titleLabel.text = recommend.tname;
    self.subscribeLabel.text = recommend.subnum;
    self.littleTitleLabel.text = recommend.title;
    self.detaailLabel.text = recommend.digest;
    
    NSString *str = recommend.tname;
    NSMutableArray *moreArray = [[FMDBSingleton FMDBDataBase] selectReacommendName];
    if ([moreArray containsObject:str])
    {
        _recommend.isCollect = YES;
        [_selectButton setImage:[UIImage imageNamed:@"reader_topic_remove_red_button@2x.png"] forState:UIControlStateNormal];
    }
    else
    {
        _recommend.isCollect = NO;
        [_selectButton setImage:[UIImage imageNamed:@"reader_topic_add_red_button@2x.png"] forState:UIControlStateNormal];
    }
}

-(void)buttonAction:(UIButton *)btn
{
    if (self.recommend.isCollect)
    {
        [[FMDBSingleton FMDBDataBase] deleteData:self.recommend.tname];
        [_selectButton setImage:[UIImage imageNamed:@"reader_topic_add_red_button@2x.png"] forState:UIControlStateNormal];
    }
    else
    {

        [[FMDBSingleton FMDBDataBase] insertRecommend:self.recommend];
        [_selectButton setImage:[UIImage imageNamed:@"reader_topic_remove_red_button@2x.png"] forState:UIControlStateNormal];
    }
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadAgain" object:nil];
    
    self.recommend.isCollect = !self.recommend.isCollect;
//    [self.delegate reloadAgain:self.recommend.tname];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
